//
//  ZSBaseTabBarController.swift
//  creativeassistant
//
//  Created by 灵工邦 on 2025/2/8.
//

import UIKit

class ZSBaseTabBarController: UITabBarController, UITabBarControllerDelegate {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self
        self.tabBar.barTintColor = UIColor.white
        self.tabBar.isTranslucent = false
        UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor(hexString: "#151B2F"), .font: UIFont.systemFont(ofSize: 10, weight: .medium)], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor(hexString: "#533DFF"), .font: UIFont.systemFont(ofSize: 10, weight: .medium)], for: .selected)
        setupChildsViewController()
    }
    
    fileprivate func setupChildsViewController() {
        let homeVC = ZSHomeViewController()
        addChildController(homeVC, "首页", "sy_nor", "sy")
        let assistantVC = ZSAssistantViewController()
        addChildController(assistantVC, "AI智能助手", "ai_ico", "ai")
        let minVC = ZSMineViewController()
        addChildController(minVC, "个人中心", "wd_nor", "wd")
    }
    
    open func addChildController(_ childController: UIViewController, _ title: String, _ image: String, _ selectImage: String) {
        childController.tabBarItem = UITabBarItem()
        childController.tabBarItem.title = title
        childController.tabBarItem.image = UIImage(named: image)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: selectImage)?.withRenderingMode(.alwaysOriginal)
        let navigationController = ZSBaseNavigationController(rootViewController: childController)
        self.addChild(navigationController)
    }
    
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if let index = tabBarController.viewControllers?.firstIndex(of: viewController), index == 1 {
            if let navController = UIApplication.topViewController()?.navigationController as? UINavigationController {
                if let orderid = UserDefaults.standard.object(forKey: "orderId") as? String, !orderid.isEmpty  {
                    let assistantVC = ZSAssistantViewController()
                    navController.pushViewController(assistantVC, animated: true)
                    return false
                }
                if let benefit = UserDefaults.standard.object(forKey: zsBenefit) as? Date, DateUtils.isDateWithinThreeDaysBeforeToday(benefit)  {
                    let assistantVC = ZSAssistantViewController()
                    navController.pushViewController(assistantVC, animated: true)
                    return false
                }
                if var signData = UserDefaults.standard.object(forKey: zsSignData) as? Dictionary<String, Any> {
                    if let number = signData["number"] as? Int, number > 0 {
                        signData["number"] = number - 1
                        UserDefaults.standard.setValue(signData, forKey: zsSignData)
                        let assistantVC = ZSAssistantViewController()
                        navController.pushViewController(assistantVC, animated: true)
                        return false
                    }
                }
                let vc = ZSSubscriptionViewController()
                vc.modalPresentationStyle = .overFullScreen
                self.present(vc, animated: false)
                return false
            }
            return false
        }
        return true
    }

}
